/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figures A.4

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/FigA4`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/AKM_Firm_FE_for_export_stata.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/
rename obs total_obs

*Winsorize (trim) Firm Fixed Effects
winsor2 firm_fe4_g0, replace cuts(1 99) trim
winsor2 firm_fe4_g1, replace cuts(1 99) trim
winsor2 firm_fe4, replace cuts(1 99) trim
winsor2 uf_fe4, replace cuts(1 99) trim

* Figure A.4 (a)
* IV Regression
ivregress 2sls uf_fe4_g0 (firm_fe4_g0 = firm_fe4_g1) [aw=total_obs] if uf_fe4_g0 != . & uf_fe4_g0!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1, first
ivregress 2sls uf_fe4 (firm_fe4_g0 = firm_fe4_g1) [aw=total_obs] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1, first

* Reduced form
reg uf_fe4_g0 firm_fe4_g1 [aw=total_obs] if uf_fe4_g0 != . & uf_fe4_g0!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1
reg uf_fe4 firm_fe4_g1 [aw=total_obs] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1

* First Stage Visualization
binscatter firm_fe4_g0 firm_fe4_g1 [aw=total_obs] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1, savedata(binnedFS) replace ///
	xtitle("Firm FE for Reg. Workers (Rand. Group 1)", size(medium)) ///
	ytitle("Firm FE for Reg. Workers (Rand. Group 0)", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) lcolor(black) mcolor(black)
graph export "${Results}/binscatter_akm4_FS.pdf", replace

*Slopes
qui reg firm_fe4_g0 firm_fe4_g1 [aw=total_obs] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4_g0 != . & firm_fe4_g0 != 0 & firm_fe4_g1 != . & firm_fe4_g1 != 0 & connected_set == 1
local beta_firm_fs = _b[firm_fe4_g1]
local se_firm_fs = _se[firm_fe4_g1]
local constant_fs  = _b[_cons]

preserve

clear
qui do binnedFS

*Plot Figure
twoway function y= 0.063+ x, range(-.3 .6) color(black) lpattern(dash) || scatter firm_fe4_g0 firm_fe4_g1, mcolor(black) msize(medlarge) || function y= `constant_fs' + `beta_firm_fs'*x , range(-.3 .6) color(black)   /*
*/ ytitle("Firm FE for Reg. Workers (Rand. Group 0)", size(medium)) xtitle("Firm FE for Reg. Workers (Rand. Group 1)", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(-.2 .6)) ysc(r(-.2 .6)) ylab(-.2(.2).6,nogrid) xlabel(-.2(.2).6) xsize(16) ysize(9) text(.3 .5 "{&beta} = 0.97 (SE 0.0029)")
graph export "${Results}/scatterplot_akm4_FS.pdf", replace

restore

*Figure A.4 (b)
*Now we plot average Regular Worker Tenure vs Regular Firm FE
binscatter tenure firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & tenure != 0 & tenure != . & uf_fe4 != 0 & uf_fe4 != . & connected_set == 1 , replace ytitle("Average Regular Worker Tenure (Months)", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) lcolor(black) mcolor(black)
graph export "${Results}/ScatterPlot_akm4_Tenure_firmFE.pdf", replace

*Slopes
qui reg tenure firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & tenure != 0 & tenure != . & uf_fe4 != 0 & uf_fe4 != . & connected_set == 1
local beta_firm_fe4_tenure = _b[firm_fe4]
local se_firm_fe4_tenure = _se[firm_fe4]

log close
